
package org.jhotdraw.samples.phototemplate;

import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import javax.swing.JDialog;
import javax.swing.JInternalFrame;
import org.jhotdraw.samples.phototemplate.validation.NumberInputVerifier;

/**
 *
 * @author Eliazar
 */


public class SizeDialog extends javax.swing.JDialog {

    private Dimension size;
    
    /**
     * Creates new form SizeDialog
     */
    public SizeDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        pnNewDimension.setVisible(false);  
        pack();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        cmbDimesion = new javax.swing.JComboBox();
        btConfirme = new javax.swing.JButton();
        pnNewDimension = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtWidth = new javax.swing.JTextField();
        txtHeight = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Dimensões do Template");

        jLabel1.setText("Dimensões do template:");

        cmbDimesion.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "254 x 203.2 mm (10x8)", "210 x 297    mm (A4)", "Outro" }));
        cmbDimesion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbDimesionActionPerformed(evt);
            }
        });

        btConfirme.setText("Confirmar");
        btConfirme.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConfirmeActionPerformed(evt);
            }
        });

        pnNewDimension.setBorder(javax.swing.BorderFactory.createTitledBorder("Novas Dimensões"));

        jLabel2.setText("Largura:");

        jLabel3.setText("Altura:");

        txtWidth.setInputVerifier(new NumberInputVerifier());

        txtHeight.setInputVerifier(new NumberInputVerifier());

        jLabel4.setText("mm");

        jLabel5.setText("mm");

        javax.swing.GroupLayout pnNewDimensionLayout = new javax.swing.GroupLayout(pnNewDimension);
        pnNewDimension.setLayout(pnNewDimensionLayout);
        pnNewDimensionLayout.setHorizontalGroup(
            pnNewDimensionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnNewDimensionLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnNewDimensionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnNewDimensionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnNewDimensionLayout.createSequentialGroup()
                        .addComponent(txtHeight, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5))
                    .addGroup(pnNewDimensionLayout.createSequentialGroup()
                        .addComponent(txtWidth, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnNewDimensionLayout.setVerticalGroup(
            pnNewDimensionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnNewDimensionLayout.createSequentialGroup()
                .addContainerGap(17, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addGap(45, 45, 45))
            .addGroup(pnNewDimensionLayout.createSequentialGroup()
                .addGroup(pnNewDimensionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnNewDimensionLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(pnNewDimensionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                            .addComponent(jLabel2)
                            .addComponent(txtWidth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnNewDimensionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtHeight, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.CENTER, pnNewDimensionLayout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(jLabel3)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnNewDimension, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbDimesion, 0, 160, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btConfirme)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cmbDimesion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnNewDimension, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btConfirme)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btConfirmeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConfirmeActionPerformed
        size = null;
        int resolution = java.awt.Toolkit.getDefaultToolkit().getScreenResolution();
        if(cmbDimesion.getSelectedIndex() == 0) {
            Double width =  UnitConv.mm2px(Double.valueOf(254).doubleValue(), resolution);
            Double height = UnitConv.mm2px(Double.valueOf(203.2).doubleValue(), resolution);
            size = new Dimension(width.intValue(), height.intValue());                                     
            setVisible(false);            
        }else if(cmbDimesion.getSelectedIndex() == 1){            
            Double width =  UnitConv.mm2px(Double.valueOf(210).doubleValue(), resolution);
            Double height = UnitConv.mm2px(Double.valueOf(297).doubleValue(), resolution);
            size = new Dimension(width.intValue(), height.intValue());                                     
            setVisible(false);
        } else {
            if(txtHeight.getText().trim().equals("") || txtWidth.getText().trim().equals("")) {
                MessageDialog.showMessageFail(null, "Informe os valores para a largura e altura do template.");
            }else {
                Double width =  UnitConv.mm2px(Double.valueOf(txtWidth.getText()).doubleValue(), resolution);
                Double height = UnitConv.mm2px(Double.valueOf(txtHeight.getText()).doubleValue(), resolution);
                size = new Dimension(width.intValue(), height.intValue());                         
                setVisible(false);
            }
        }               
    }//GEN-LAST:event_btConfirmeActionPerformed
  
    
    private void cmbDimesionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbDimesionActionPerformed
        txtHeight.setText("");
        txtWidth.setText("");
        if(cmbDimesion.getSelectedIndex() == 2) {
            pnNewDimension.setVisible(true);
        }else {
            pnNewDimension.setVisible(false);
        }
        pack();
    }//GEN-LAST:event_cmbDimesionActionPerformed

    public Dimension getSize() {
        return  size;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SizeDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SizeDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SizeDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SizeDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                SizeDialog dialog = new SizeDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btConfirme;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cmbDimesion;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel pnNewDimension;
    private javax.swing.JTextField txtHeight;
    private javax.swing.JTextField txtWidth;
    // End of variables declaration//GEN-END:variables
}
